/*
 * @Author: xinyang3
 * @Date: 2021-11-22 09:08
 * @Descripttion: awesome description
 * @LastEditors: xinyang3
 * @LastEditTime: 2021-11-22 10:53
 */
import path from 'path';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import webpack from 'webpack';

var webpackProdConfig = {
  mode: 'production',
  entry: {
    app: ['./app.js'],
  },
  target: 'node',
  node: {
    global: true,
    __dirname: true,
    __filename: true,
  },
  // 开发环境下找到错误、警告位置
  // devtool: 'inline-source-map',
  plugins: [
    // 打包前 清理out文件夹
    new CleanWebpackPlugin(),
    new webpack.ids.HashedModuleIdsPlugin({
      hashFunction: 'sha256',
      hashDigest: 'hex',
      hashDigestLength: 20,
    }),
  ],
  output: {
    filename: '[name].js',
    chunkFilename: '[name].bundle.js',
    path: path.resolve('build'),
  },
  module: {
    rules: [
      // {
      //   test: /\.css$/,
      //   use: ['style-loader', 'css-loader']
      // },
      // {
      //   test: /\.(png|svg|jpg|gif)$/,
      //   use: ['file-loader']
      // },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: ['file-loader'],
      },
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: [
              '@babel/plugin-proposal-object-rest-spread',
              '@babel/plugin-transform-runtime',
            ],
            cacheDirectory: true,
          },
        },
      },
    ],
  },
  optimization: {
    minimize: true,
    moduleIds: 'named',
    runtimeChunk: 'single',
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendor',
          chunks: 'all',
          priority: -10,
        },
        common: {
          name: 'common',
          chunks: 'all',
          minChunks: 2, //模块被引用2次以上的才抽离
          priority: -20,
        },
      },
    },
  },
};

export default webpackProdConfig;
